package com.lywz.biz.dao.hotel.impl;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.stereotype.Repository;

import com.lywz.biz.common.dao.AbstractBaseDao;
import com.lywz.biz.common.entity.hotel.Hotel;
import com.lywz.biz.dao.hotel.HotelDao;
import com.lywz.core.util.CommonDefaultCheck;

/**
 * 住宿Dao的实例
 * 
 * @author Sunna
 * @date 2010-5-7 下午07:04:57
 */
@Repository("hotelDao")
public class HotelDaoImpl extends AbstractBaseDao implements HotelDao {

    /*
     * (non-Javadoc)
     * @see com.lywz.biz.dao.hotel.HotelDao#queryHotelByCityId(int, int, int)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Collection<Hotel> queryHotelsByCityId(int cityId, int first, int max) throws Exception {
        Query q = session().getNamedQuery("hotel.queryHotelByCityId");
        q.setInteger("cityId", cityId);
        q.setFirstResult(first);
        q.setMaxResults(max);
        return (Collection<Hotel>) CommonDefaultCheck.checkDefault(q.list()); // 该方法屏蔽了q.list()为空的情况，自动返回空List。
    }

    /*
     * (non-Javadoc)
     * @see com.lywz.biz.dao.hotel.HotelDao#queryHotelByScenicId(int, int, int)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Collection<Hotel> queryHotelsByScenicId(int scenicId, int first, int max)
            throws Exception {
        Query q = session().getNamedQuery("hotel.queryHotelByScenicId");
        q.setInteger("scenicId", scenicId);
        q.setFirstResult(first);
        q.setMaxResults(max);
        return (Collection<Hotel>) CommonDefaultCheck.checkDefault(q.list());
    }

    /*
     * (non-Javadoc)
     * @see com.lywz.biz.dao.hotel.HotelDao#queryHotelByCompose(int, int, java.lang.String, int, int, int, int, int)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Collection<Hotel> queryHotelsByCompose(int cityId, int countryId, String name, int type,
            int bprize, int ePrize, int first, int max) throws Exception {

        //        if (0 == cityId) cityId = -1;
        //        if (0 == countryId) countryId = -1;
        //        if (name.equals(null))
        //        if (0 == type) type = -1;

        Criteria c = session().createCriteria(Hotel.class);

        if (0 != cityId) {
            c.add(Restrictions.eq("cityId", cityId));
        }
        if (0 != countryId) {
            c.add(Restrictions.eq("countryId", countryId));
        }
        if (!name.equals(null) || name.equals("")) {
            c.add(Restrictions.eq("name", name));
        }
        if (0 != type) {
            c.add(Restrictions.eq("cityId", type));
        }
        if (0 != bprize) {
            c.add(Restrictions.eq("bprize", bprize));
        }
        if (0 != ePrize) {
            c.add(Restrictions.eq("ePrize", ePrize));
        }
        c.setFirstResult(first);
        c.setMaxResults(max);
        return (Collection<Hotel>) CommonDefaultCheck.checkDefault(c.list());
    }
    /*
     * (non-Javadoc)
     * @see com.lywz.biz.dao.hotel.HotelDao#queryHotelByCompose(int, int, java.lang.String, int, int, int, int, int)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Collection<Hotel> queryHotelByCompose(HotelConditionGenerater generater, int startRecord, int recordSize) throws Exception {
    	Criteria criteria = session().createCriteria(Hotel.class);
    	
    	for(SimpleExpression expression: generater.getConditionList()){
    		criteria.add(expression);
    	}
    	
    	criteria.setFirstResult(startRecord);
    	criteria.setMaxResults(recordSize);
    	return (Collection<Hotel>) CommonDefaultCheck.checkDefault(criteria.list());
    }
}
